<?php
// +----------------------------------------------------------------------
// | 多客怪兽框架多平台开发框架 [ 以下开发工具自动生成-版权勿删 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021~2024 http://www.dkyx.cc All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 刘泓宾（514660094@qq.com）
// +----------------------------------------------------------------------

namespace addons\cloud_market\model;

use think\model\concern\SoftDelete;

/**
 * 站点应用模型文件
 * Create by :  刘泓宾<514660094@qq.com>
 * Copyright :  copyright (c) 多客怪兽, www.dkyx.cc
 */
class CloudMarketSiteAddon extends CloudMarketBaseModel
{
    // 配置软删除、启用回收站
    use SoftDelete;
    protected $deleteTime = "delete_time";

    // 字段自动类型转换
    protected $type = array(

    );
    // 数据输出需要追加的属性
    protected $append = array(

    );

    public $childFields = ['site_id'];
    public function __construct(array $data = [])
    {
        parent::__construct($data);
    }


    /**
     * 关联CloudMarketSite模型
     * Create by :  刘泓宾<514660094@qq.com>
     * @return \think\model\relation\BelongsTo
     */
    public function site()
    {
        return $this->belongsTo(CloudMarketSite::class, 'site_id', 'id');
    }

    /**
     * 关联CloudMarketAddon模型
     * Create by :  刘泓宾<514660094@qq.com>
     * @return \think\model\relation\BelongsTo
     */
    public function addon()
    {
        return $this->belongsTo(CloudMarketAddon::class, 'addon_id', 'id');
    }


    /**
     * 人性化时间
     * @param $value
     * @return string
     */
    protected function getServicetimeManAttr($value,$row)
    {
        if ($row['servicetime'] > time()) {
            $day = ($row['servicetime']  - time()) / (24 * 60 * 60);
            if ($day < 1) {
                return "剩余" . intval($day * 24) . "小时";
            } elseif ($day < 30) {
                return "剩余" . intval($day) . "天";

            } else {
                return date("Y-m-d H:i:s",$row['servicetime']);
            }
        } else {
            return "已过期";
        }
    }

    public function getUsedCountAttr($value, $data)
    {
        return CloudMarketSiteAddonToken::where('site_id', $data['site_id'])->where('aid', $data['addon_id'])->count();
    }

}
